package com.kaiyidiannao.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.SecondHandCar;
import com.kaiyidiannao.model.bean.UserBean;
import com.kaiyidiannao.model.req.model.PostDeleteMessageInfo;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mark on 14-4-11.
 */
public class SecondHandCarAdapter extends ImageLoaderBaseAdapter {
    private ImageLoader imageLoader;
    private Context context;
    private LayoutInflater layoutInflater;
    private List<SecondHandCar> list = new ArrayList<SecondHandCar>();
    ArrayList<UserBean> userBeans = DataCenter.userLogin;
    private int typeid = DataCenter.typeid;

    public SecondHandCarAdapter(Context context, List<SecondHandCar> list) {
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
        this.context = context;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ImageView imageView = null;
        TextView nameTextView = null;
        TextView priceTextView = null;
        TextView howNewTextView = null;
        TextView carAgeTextView = null;
        TextView howLongTextView = null;
        TextView callNameTextView = null;
        TextView popularityTextView = null;
        TextView publishTime;
        ImageButton btDelete;
        TextView picPrice;
        LinearLayout call;
        ImageView callNum;
        TextView hits;
        final SecondHandCar secondHandCar = list.get(position);
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.secondhandcar_item, null);
        }
        btDelete = (ImageButton) convertView.findViewById(R.id.btDelete);
        hits = (TextView) convertView.findViewById(R.id.hits);
        picPrice = (TextView) convertView.findViewById(R.id.picPrice);
        imageView = (ImageView) convertView.findViewById(R.id.secondHandCarPic);
        nameTextView = (TextView) convertView.findViewById(R.id.secondHandCarName);
        priceTextView = (TextView) convertView.findViewById(R.id.secondHandCarPrice);
        howNewTextView = (TextView) convertView.findViewById(R.id.secondHandCarHowNew);
        carAgeTextView = (TextView) convertView.findViewById(R.id.carAge);
        howLongTextView = (TextView) convertView.findViewById(R.id.howLong);
        callNameTextView = (TextView) convertView.findViewById(R.id.callName);
        popularityTextView = (TextView) convertView.findViewById(R.id.popularity);
        callNum = (ImageView) convertView.findViewById(R.id.callNum);
        publishTime = (TextView)convertView.findViewById(R.id.publishTime);
        call = (LinearLayout)convertView.findViewById(R.id.callcar);
        imageLoader.displayImage(secondHandCar.getThumb(), imageView, options, null);
        nameTextView.setText(secondHandCar.getTitle());

        btDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (typeid == 64) {
                    String action = "delete";
                    DataCenter.position = position;
                    ((BaseActionBarActivity) context).showProgressDialog();
                    new PostDeleteMessageInfo(secondHandCar.getFid(), userBeans.get(0).getUserid(), action).execute();
                }
            }
        });

        if (secondHandCar.getMileage() == null || secondHandCar.getMileage().equals("")) {
            howLongTextView.setVisibility(View.GONE);
        } else {
            howLongTextView.setText(secondHandCar.getMileage() + "万公里");
        }
        howNewTextView.setText(secondHandCar.getCondition());
        carAgeTextView.setText(secondHandCar.getCaryear() + "年");
        if (secondHandCar.getPrice() == null || secondHandCar.getPrice().equals("")) {
            priceTextView.setVisibility(View.GONE);
            picPrice.setVisibility(View.GONE);
        } else {
            priceTextView.setText(secondHandCar.getPrice() + "万");
        }

        callNameTextView.setText(TextUtils.isEmpty(secondHandCar.getUsername()) ? "匿名" : secondHandCar.getUsername());
        callNameTextView.setText(secondHandCar.getUsername());

            popularityTextView.setText(secondHandCar.getHits());


        if (null != call){
            call.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String phoneNum =  secondHandCar.getTelephone();
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_CALL);
                    intent.setData(Uri.parse("tel:" + phoneNum));
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                }
            });
        }

//        publishTime.setText(secondHandCar.getAddtime());

        return convertView;
    }
}
